package com.atguigu.twoSum;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * 力扣第一题：两数之和
 */
public class twoSum {

    public static int[] mapSum(int[] arrays, int target) {

        Map<Integer,Integer> hashMap = new HashMap<>(arrays.length);

        for (int i = 0; i < arrays.length; i++) {
            int p = target - arrays[i];
            if (hashMap.containsKey(p)){
                return new int[]{hashMap.get(p),i};
            }
            hashMap.put(arrays[i],i);
        }
        return null;
    }

    public static int[] sum(int[] arrays, int target) {

        for (int i = 0; i < arrays.length; i++) {
            for (int j = 0; j < arrays.length; j++) {
                if (arrays[i] + arrays[j] == target) {
                    return new int[]{i, j};
                }
            }
        }
        return null;
    }

    public static void main(String[] args) {
        int[] arrays = new int[]{1, 11, 22, 33, 44, 99, 102};
        int target = 12;

        Arrays.toString(mapSum(arrays, target));
    }
}
